package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.legal.domain.vo.AjtzBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Ajtz;
import com.ruoyi.legal.service.IAjtzService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案件台账Controller
 *
 * @author xwj
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/legal/ajtz")
@Api(tags = "案件台账")
public class AjtzController extends BaseController {
    @Autowired
    private IAjtzService ajtzService;

    /**
     * 查询案件台账列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Ajtz ajtz) {
        startPage();
        List<Ajtz> list = ajtzService.selectAjtzList(ajtz);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Ajtz ajtz) {
        startOrderBy();
        List<Ajtz> list = ajtzService.selectAjtzList(ajtz);
        return success(list);
    }

    @PostMapping("/countFmks")
    @ApiOperation("条件查询-罚没款数")
    public AjaxResult countFmks(@RequestBody Ajtz ajtz) {
        Double num = ajtzService.countFmks(ajtz);
        return success(num);
    }

    /**
     * 导出案件台账列表
     */
    @Log(title = "案件台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Ajtz ajtz) {
        List<Ajtz> list = ajtzService.selectAjtzList(ajtz);
        ExcelUtil<Ajtz> util = new ExcelUtil<Ajtz>(Ajtz.class);
        util.exportExcel(response, list, "案件台账数据");
    }

    /**
     * 获取案件台账详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(ajtzService.selectAjtzById(id));
    }


    @PostMapping("/batchSaveUpdate")
    @ApiOperation("批量保存")
    public ResponseModel<AjtzBatchVo> batchSaveUpdate(@RequestBody AjtzBatchVo ajtzBatchVo) {
        SysUser user = getLoginUser().getUser();
        ajtzService.batchSaveUpdate(ajtzBatchVo, user);

        return ResponseModel.of(ajtzBatchVo);
    }

    /**
     * 新增案件台账
     */
    @Log(title = "案件台账", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Ajtz ajtz) {
        SysUser user = getLoginUser().getUser();
        if (ajtz.getId() == null) {
            ajtz.setStatus("0"); // 0未提交，1已提交
            ajtz.setCreateId(user.getUserId());
            ajtz.setCreateTime(DateUtils.getNowDate());
            ajtz.setCreateBy(user.getNickName());

            if(StringUtils.isEmpty(ajtz.getLasj())) {
                String nowStr = DateUtil.formatDateTime(DateUtil.date());
                ajtz.setLasj(nowStr);
            }

            ajtzService.insertAjtz(ajtz);
        } else {
            ajtz.setUpdateId(user.getUserId());
            ajtz.setUpdateTime(DateUtils.getNowDate());
            ajtz.setUpdateBy(user.getNickName());
            ajtzService.updateAjtz(ajtz);
        }

        return success(ajtz);
    }

    /**
     * 修改案件台账
     */
    @Log(title = "案件台账", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Ajtz ajtz) {
        SysUser user = getLoginUser().getUser();
        ajtz.setUpdateId(user.getUserId());
        ajtz.setUpdateTime(DateUtils.getNowDate());
        ajtz.setUpdateBy(user.getNickName());
        return success(ajtzService.updateAjtz(ajtz));
    }

    /**
     * 删除案件台账
     */
    @Log(title = "案件台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ajtzService.deleteAjtzByIds(ids));
    }

    @Log(title = "案件台账", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Ajtz ajtz) {
        int num = ajtzService.delete(ajtz);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Ajtz>> queryPage(@RequestBody Ajtz ajtz) {
        Pages<Ajtz> page = ajtzService.queryPage(ajtz);
        return ResponseModel.of(page);
    }
}
